/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
 **/
package awd.bj.jls.webs.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

import awd.bj.jls.model.TsdjModel;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.PagerResult;
import awd.bj.jls.webs.utils.QueryParam;
import awd.bj.jls.webs.utils.ResponseMessage;
import awd.bj.jls.webs.utils.Result;
import awd.bj.jls.webs.utils.ResultUtils;
import awd.bj.jls.webs.utils.StringUtils;

@Controller
@RequestMapping("/tsdj")
@RefreshScope
public class TsdjAction extends BaseAction {

    @Autowired
    private JlsServersApi jlsServersApi;

    @RequestMapping({"/tsdj.html" //首页
            , "/tsdj_enter.html" //单表录入页面
            , "/tsdj_jbxx_enter.html" //人员信息查询页面
            , "/tsdj_query.html" //信息查询
            , "/tsdj_jbxx_query.html" //关联基本信息查询
            , "/6.html" //
            , "/7.html"})
    public String index(Model model, HttpServletRequest request) {
        model.addAllAttributes(getBaseModelData());
        return getRenderFilePath(request);
    }

    /**
     * 数据列表,关联基本信息
     * @param request
     * @param params 所有请求的参数集合
     * @return
     */
    @RequestMapping("/tsdjAndJbxxList")
    @ResponseBody
    public Map<String, Object> list2jbxx(HttpServletRequest request, Map<String, Object> params) {
        QueryParam param = getBaseQueryParam(request, params);
        ResponseMessage<PagerResult<Map<String, Object>>> list = jlsServersApi.getTsdjJbxxList(param);
        Map<String, Object> result = Maps.newHashMap();
        result.put("total", list.getResult().getTotal());
        result.put("rows", list.getResult().getData());
        return result;
    }

    /**
     * 数据列表
     * @param request
     * @param params 所有请求的参数集合
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request, Map<String, Object> params) {
        QueryParam param = getBaseQueryParam(request, params);
        System.out.println("\33[34;4m张天啸，提讯打印数据："+JSON.toJSONString(param)+"\33[30;0m");
        ResponseMessage<PagerResult<TsdjModel>> list = jlsServersApi.tsdjQueryForPage(param);
        Map<String, Object> result = Maps.newHashMap();
        result.put("total", list.getResult().getTotal());
        result.put("rows", list.getResult().getData());
        return result;
    }

    /**
     * 数据保存操作
     * @param model
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result<?> save(@ModelAttribute TsdjModel model) {
        User users = getCurrentUser();
        try {
            model.setState("R2");
            model.setJsbh(users.getJsbh());
            model.setCreatetime(new Date());
            model.setCreator(users.getUsername());
            System.err.println("王涛:提讯登记呈批——保存数据======================================");
            System.err.println(JSON.toJSONString(model));
            System.err.println("================================================================");
            return ResultUtils.success(jlsServersApi.tsdjSave(model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    /**
     * 数据更新
     * @param model
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result<?> update(@ModelAttribute TsdjModel model) {
        User users = getCurrentUser();
        try {
            model.setUpdator(users.getUsername());
            model.setUpdatetime(new Date());
            return ResultUtils.success(jlsServersApi.tsdjUpdate(model.getId(), model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    /**
     * 删除操作
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result<?> delete(@ModelAttribute TsdjModel model) {
        User users = getCurrentUser();
        try {
            model.setState("R3");
            model.setUpdator(users.getUsername());
            model.setUpdatetime(new Date());
            return ResultUtils.success(jlsServersApi.tsdjUpdate(model.getId(), model));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "删除失败");
        }
    }

    /**
     * 核对凭证保存
     * @param model
     * @return
     */
    @RequestMapping("/hdpzSave")
    @ResponseBody
    public Result<?> hdpzSave(@ModelAttribute TsdjModel model) {
        User users = getCurrentUser();
        try {
            model.setState("R2");
            model.setBllx("2");//非预约
            model.setJsbh(users.getJsbh());
            model.setCreatetime(new Date());
            model.setCreator(users.getUsername());
            ResponseMessage<String> responseMessage = jlsServersApi.hdpzSave(model);
            return ResultUtils.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    /**
     * 登记呈批保存
     * @param model
     * @return
     */
    @RequestMapping("/djcpSave")
    @ResponseBody
    public Result<?> djcpSave(HttpServletRequest request, @ModelAttribute TsdjModel model) {
        User users = getCurrentUser();
        if(StringUtils.isNullOrEmpty(model.getRybh())){
            return ResultUtils.error("人员编号未传！");
        }
        String taskid = request.getParameter("taskid");
        String ywlcid = request.getParameter("ywlcid");
        try {

            model.setJsbh(users.getJsbh());
            ResponseMessage<String> responseMessage = jlsServersApi.djcpSave(taskid, ywlcid, model);
            return ResultUtils.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }

    /**
     * 安排提询保存
     * @param model
     * @return
     */
    @RequestMapping("/aptxSave")
    @ResponseBody
    public Result<?> aptxSave(HttpServletRequest request, @ModelAttribute TsdjModel model) {
        User users = getCurrentUser();
        if(StringUtils.isNullOrEmpty(model.getRybh())){
            return ResultUtils.error("人员编号未传！");
        }
        String taskid = request.getParameter("taskid");

        try {

            model.setJsbh(users.getJsbh());
            ResponseMessage<String> responseMessage = jlsServersApi.aptxSave(taskid, model);
            return ResultUtils.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }
    /**
     * 询完登记
     * @param model
     * @return
     */
    @RequestMapping("/xwdjSave")
    @ResponseBody
    public Result<?> xwdjSave(HttpServletRequest request, @ModelAttribute TsdjModel model) {
        User users = getCurrentUser();
        if(StringUtils.isNullOrEmpty(model.getRybh())){
            return ResultUtils.error("人员编号未传！");
        }
        String taskid = request.getParameter("taskid");
        try {
            model.setJsbh(users.getJsbh());
            ResponseMessage<String> responseMessage = jlsServersApi.xwdjSave(taskid, model);
            return ResultUtils.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }
    /**
     * 所长意见
     * @param model
     * @return
     */
    @RequestMapping("/szyjSave")
    @ResponseBody
    public Result<?> szyjSave(HttpServletRequest request, @ModelAttribute TsdjModel model) {
        User users = getCurrentUser();
        if(StringUtils.isNullOrEmpty(model.getRybh())){
            return ResultUtils.error("人员编号未传！");
        }
        String taskid = request.getParameter("taskid");
        try {
            model.setJsbh(users.getJsbh());
            ResponseMessage<String> responseMessage = jlsServersApi.szyjSave(taskid, model);
            return ResultUtils.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
    }
}